/* Maps */
var Maps = {
	baseURL: 'maps/',
	list:{
		level1:{file:'level1.json'}
	},
	_loaded:function(t){
		var c = this.context?this.context:this;
		c.data = t;
		c.json = JSON.parse(t);
		var m = Maps.createFromJSON(c.json);
		$('body').trigger('MapData',[m]);
		if (m.loading.length < 1){
			$('body').trigger('MapLoaded',[m]);
		}
	},
	_error:function(){
		$('body').trigger('MapError');
	},
	_resourceLoaded:function(){
		var loading= this._map.loading,
			resources = this._map.resources,
			item = loading.splice(loading.indexOf(this))[0];
		resources.push(item);
		item.loaded = true;
		$('body').trigger('MapResourceLoaded',[this._map,item]);
		if (loading.length < 1){
			$('body').trigger('MapLoaded',[this._map]);
		}
	},
	_resourceError:function(){
		var loading= this._map.loading,
		item = loading.splice(loading.indexOf(this))[0];
		this._map.failedResources.push(item);
		$('body').trigger('MapResourceFailed',[this._map, item]);
		if (loading.length < 1){
			$('body').trigger('MapLoaded',[this._map]);
		}
	},
	_mapPackLoaded:function(t){
		try {
		var c = this.context?this.context:this,
			json= JSON.parse(t);
		}catch(err){}
		if (!json){
			Maps._mapPackError();
			return;
		}
		Maps.list = json;
		if (m.loading.length < 1){
			$('body').trigger('MapPackLoaded',[m]);
		}
	},
	_mapPackError:function(){
		$('body').trigger('MapPackError');
	},
	createFromJSON:function(json){
		var m = new Map(json.width,json.height,json.blockSize),
			ns= json.nodes,
			nt= json.nodeTypes,
			ts= {};
		m.starts = [];
		m.ends   = [];
		m.types = nt;
		m.resources = [];
		m.failedResources = [];
		m.loading = [];
		if (json.backgroundImage){
			var img = new Image();
			img._map = m;
			m.loading.push(img);
			img.addEventListener('load',Maps._resourceLoaded,false);
			img.addEventListener('error',Maps._resourceError,false);
			img.src = Maps.baseURL+json.backgroundImage;
			m.backgroundImage = img;
		}
		if (json.spriteMap){
			var img = new Image();
			img._map = m;
			m.loading.push(img);
			img.addEventListener('load',Maps._resourceLoaded,false);
			img.addEventListener('error',Maps._resourceError,false);
			img.src = Maps.baseURL+json.spriteMap;
			m.spriteMap = img;
		}
		for (var i in nt){
			ts[nt[i].value+''] = i;
		}
		for (var i = ns.length; i--;){
			var n = ns[i];
			for (var j = n.length;j--;){
				var t = n[j],
					node = m.getNode(j,i);
				if ((typeof t).toLowerCase() !== 'string')
					t = ts[t+''];
				node.type = t;
				switch (t){
					case 'START':
						m.starts.push(node);
						break;
					case 'END':
						m.ends.push(node);
				}
			}
		}
		return m;
	},
	load:function(m){
		switch((typeof m).toLowerCase()){
			case 'string':
				m = Maps.list[m];
		}
		if (!m)
			return;
		if (m.json){
			$('body').trigger('MapData',[makeMap(m.json)]);
			return;
		}
		$.ajax({
			url:Maps.baseURL+m.file,
			type:'GET',
			dataType:'TEXT',
			context:m,
			success:Maps._loaded,
			error:Maps._error
		});
	},
	loadMapPack:function(url,base){
		base = base !== null ? base : Maps.baseURL;
		Maps.baseURL = base;
		$.ajax({
			url:base+url,
			type:'GET',
			dataType:'TEXT',
			success:Maps._mapPackLoaded,
			error:Maps._mapPackError
		});
	}
}

/* debug code */
$("body").live(
	'MapData MapLoaded MapResourceLoaded MapResourceFailed MapPackLoaded MapPackFailed',
	function(e){
		if (window.debug === true){
			console.log(+new Date, e.type);
		}
	});